﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.ComponentModel;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Outputs Labels in view mode and TextBoxes in edit mode.
	/// </summary>
	public class TextBoxColumn : BaseDataColumn
	{
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		public TextBoxColumn()
		{
			this.Validate = false;
		}
		#endregion
		#region EVENTS
		#endregion
		#region PROPS
		/// <summary>
		/// Set to true to output a <see cref="CompareValidator"/> for this <see cref="TextBox"/>, false otherwise.
		/// </summary>
		[DefaultValue(false)]
		public bool Validate { get; set; }
		#endregion
		#region METHODS
		/// <summary>
		/// Creates the view mode Label.
		/// </summary>
		/// <param name="container"></param>
		public override void CreateViewItem(System.Web.UI.Control container, Grid owner)
		{
			if (string.IsNullOrEmpty(ViewControl.Id))
				ViewControl.Id = "lbl" + Regex.Replace(this.Data.Field, "[^\\w]", "");
			Label l = new Label
			{
				ID = ViewControl.Id,
				CssClass = ViewControl.CssClass
			};
			l.Data.Set(Data);
			l.Attributes["style"] = ViewControl.Style;
			container.Controls.Add(l);
		}
		/// <summary>
		/// Creates the edit mode TextBox and then validators (if any).
		/// </summary>
		/// <param name="container"></param>
		public override void CreateEditItem(System.Web.UI.Control container, Grid owner)
		{
			if (string.IsNullOrEmpty(EditControl.Id))
				EditControl.Id = "txt" + Regex.Replace(this.Data.Field, "[^\\w]", "");
			TextBox t = new TextBox
			{
				ID = EditControl.Id,
				CssClass = EditControl.CssClass
			};
			t.Data.Set(Data);
			t.Attributes["style"] = EditControl.Style;
			container.Controls.Add(t);

			CreateValidators(container, t);
		}
		#endregion
		#endregion
		#region INTERNAL
		#region CONST/STATIC
		#endregion
		#region VARS
		#endregion
		#region PROPS
		#endregion
		#region METHODS
		#region INIT
		/// <summary>
		/// Creates the validators (if any).
		/// </summary>
		/// <param name="container"></param>
		/// <param name="field"></param>
		protected void CreateValidators(Control container, Control field)
		{
			if (Required)
			{
				RequiredValidator reqv = new RequiredValidator
				{
					ID = "reqv" + field.ID,
					ValidatedControlId = field.ID,
					InlineMessage = RequiredErrorMessage,
					Group = ValidationGroup

				};
				container.Controls.Add(reqv);
			}
			if (Validate)
			{
				CompareValidator compv = new CompareValidator
				{
					ID = "compv" + field.ID,
					ValidatedControlId = field.ID,
					InlineMessage = RequiredErrorMessage,
					Group = ValidationGroup,
					Operator = ValidationCompareOperator.DataTypeCheck,
					Type = Data.StoringType,
				};
				container.Controls.Add(compv);
			}
		}
		#endregion
		#region DATA
		#endregion
		#region HANDLERS
		#endregion
		#endregion
		#endregion				
				
	}
}
